<?php
class M_qin_album extends Cemvc_Db_MysqlDb{
  public $PrimaryKey='id';
  /*CEPHP会在要需要时查询主键，但主动设置主键可以使程序执行更快*/

  public function __construct()
  {
    parent::__construct();
  }
  public function get($id)
  {
    return $this->where(array("id = $id"))->fetchOne();
  }

  /* get all the album with the specified user id */
  public function get_album($user_id, $state=RESOURCE_STATE_INIT)
  {
    $where = array();
    $where[] = "user_id = $user_id";
    if ($state)
      $where[] = "state = '$state'";

    return $this->where($where)->orderBy("created")->fetchAll();
  }

  public function clear_cache($photo_id)
  {
    $photo = $this->get($photo_id);
    M_Tools::del_cache($this->LastQueryString);	

    /* get init state albums */
    $this->get_album($photo["user_id"]);
    M_Tools::del_cache($this->LastQueryString);	

    /* get all state albums */
    $this->get_album($photo["user_id"], null);
    M_Tools::del_cache($this->LastQueryString);	

    $history = new M_qin_history();
    $history->get_history_with_content(CONTENT_TYPE_PHOTO, $photo_id);
    M_Tools::del_cache($history->LastQueryString);
  }
  /* insert a photo to album table */
  public function add(array $array)
  {
    $res = $this->insert(array("user_id" => $array["user_id"],
			       "photo"   => $array["photo"],
			       "thumb"   => $array["thumb"],
			       "memo"    => $array["memo"]));

    $this->clear_cache($res["last_insert_id()"]);

    $photo = $this->get($res["last_insert_id()"]);

    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_PHOTO,
    			"content_id"   => $photo["id"],
    			"content"      => base64_encode(serialize($photo)),
    			"operate_type" => OPERATE_TYPE_ADD,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $photo;
  }

  public function edit($id, $array)
  {
    $this->where(array("id = $id"))->update(array("memo"=>addslashes($array["memo"])));

    $this->clear_cache($id);

    $photo = $this->get($id);
    
    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_PHOTO,
    			"content_id"   => $photo["id"],
    			"content"      => base64_encode(serialize($photo)),
    			"operate_type" => OPERATE_TYPE_EDIT,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $photo;
    
  }

  public function delete($id)
  {
    $this->where(array("id = $id"))->update(array("state"=>RESOURCE_STATE_DELETED));

    $this->clear_cache($id);

    $photo = $this->get($id);
    
    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_PHOTO,
    			"content_id"   => $photo["id"],
    			"content"      => base64_encode(serialize($photo)),
    			"operate_type" => OPERATE_TYPE_DELETE,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $photo;
    
  }
  public function restore($photo_id, $history_id)
  {
    /* get history data and set state to init */
    $history = new M_qin_history();
    $res = $history->get($history_id);
    $array = unserialize(base64_decode($res["content"]));
    $this->where(array("id = $photo_id"))->update(array("memo"=>addslashes($array["memo"]),
							"state"=>RESOURCE_STATE_INIT));

    /* clear cache */
    $this->clear_cache($photo_id);

    /* add to history */
    $photo = $this->get($photo_id);
    $history->add(array("content_type" => CONTENT_TYPE_PHOTO,
    			"content_id"   => $photo["id"],
    			"content"      => base64_encode(serialize($photo)),
    			"operate_type" => OPERATE_TYPE_RESTORE,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $photo;
  }

  /* select 30 random photos from album */
  public function get_some_photos()
  {
    return $this->setField(array("user_id", "thumb", "realname"))
      ->where(array("thumb!=''", "state='".RESOURCE_STATE_INIT."'"))
      ->orderBy("rand()")
      ->limit(MAX_PHOTO_NUM_ON_PHOTO_WALL)
      ->fetchAll();
  }
}